//
//  SACommodityAddShopSizeCell.m
//  yidiantong
//
//  Created by 黄志刚 on 2020/11/23.
//

#import "SACommodityAddShopSizeCell.h"
#import "SASalesOrderViewListModel.h"

@interface SACommodityAddShopSizeCell ()<UITextFieldDelegate>

@property (nonatomic, strong) UILabel *sizeLab;
@property (nonatomic, strong) UIImageView *lineImageV;
@property (nonatomic, strong) UIButton *cutBtn;
@property (nonatomic, strong) UIButton *addBtn;
@property (nonatomic, strong) UITextField *numTf;
@property (nonatomic, strong) UILabel *inventoryLab;

@end

@implementation SACommodityAddShopSizeCell

-(instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier{
    if(self = [super initWithStyle:style reuseIdentifier:reuseIdentifier]){
        self.selectionStyle = UITableViewCellSelectionStyleNone;
        [self initUI];
    }
    return self;
}

-(void)setSkuModel:(SASalesOrderToPendSkuList *)skuModel {
    _skuModel = skuModel;
    self.sizeLab.text = skuModel.sizeName;
    self.numTf.text = skuModel.stock;
    self.inventoryLab.text = skuModel.inventoryCount;
}

-(void)clickedCutBtn {
    NSInteger num = [self.skuModel.stock integerValue];
    if (self.isWarehousingStock == YES) {
        self.numTf.text = [NSString stringWithFormat:@"%zd",num - 1];
    }else {
        if (num > 0) {
            self.numTf.text = [NSString stringWithFormat:@"%zd",num - 1];
        }else {
            self.numTf.text = @"0";
        }
    }
    !self.cutBlock ?: self.cutBlock();
}

-(void)clickedAddBtn {
    NSInteger num = [self.skuModel.stock integerValue];
    if (self.isNegative == YES) {//禁止负库存销售
        if (num + 1 > [self.skuModel.inventoryCount integerValue]) {
            if ([self.skuModel.inventoryCount integerValue] >= 0) {
                self.numTf.text = self.skuModel.inventoryCount;
            }else {
                self.numTf.text = @"0";
            }
            !self.addBlock ?: self.addBlock();
            return;
        }
    }
    self.numTf.text = [NSString stringWithFormat:@"%zd",num + 1];
    !self.addBlock ?: self.addBlock();
}

- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string {
    if (string.length == 0) {
        return YES;
    }
    if (textField.text.length > 5){
        return NO;
    }
    if (self.isWarehousingStock == YES && [string isEqualToString:@"-"])
    {
        NSCharacterSet* tmpSet = [NSCharacterSet characterSetWithCharactersInString:@"-"];
        NSRange range2 = [textField.text rangeOfCharacterFromSet:tmpSet];
        if (range2.length > 0) {
            return NO;
        }else {
            if (range.location == 0) {
                return YES;
            }else {
                return NO;
            }
        }
    }
    return [self validateNumber:string];
}

- (void)textFieldEditChanged:(UITextField *)textField {
    if (textField.text.length == 0) {
        !self.inputBlock ?: self.inputBlock(@"");
    }else if ([textField.text isEqualToString:@"-"]) {
        !self.inputBlock ?: self.inputBlock(@"-");
    }else {
        NSInteger newNum = [textField.text integerValue];
        if (self.isNegative == YES) {
            if (newNum > [self.skuModel.inventoryCount integerValue]) {
                if ([self.skuModel.inventoryCount integerValue] >= 0) {
                    textField.text = self.skuModel.inventoryCount;
                }else {
                    textField.text = @"0";
                }
                [Prompt popPromptViewWithMsg:[NSString stringWithFormat:@"库存不足,当前库存%@件",self.skuModel.inventoryCount] duration:2];
            }else {
                textField.text = [NSString stringWithFormat:@"%zd",newNum];
            }
        }else {
            textField.text = [NSString stringWithFormat:@"%zd",newNum];
        }
        !self.inputBlock ?: self.inputBlock(textField.text);
    }
}

- (BOOL)validateNumber:(NSString*)number {
    BOOL res = YES;
    NSCharacterSet* tmpSet = [NSCharacterSet characterSetWithCharactersInString:@"0123456789"];
    int i = 0;
    while (i < number.length) {
        NSString * string = [number substringWithRange:NSMakeRange(i, 1)];
        NSRange range = [string rangeOfCharacterFromSet:tmpSet];
        if (range.length == 0) {
            res = NO;
            break;
        }
        i++;
    }
    return res;
}

-(void)initUI {
    [self.contentView addSubview:self.lineImageV];
    [self.contentView addSubview:self.sizeLab];
    [self.contentView addSubview:self.cutBtn];
    [self.contentView addSubview:self.addBtn];
    [self.contentView addSubview:self.numTf];
    [self.contentView addSubview:self.inventoryLab];
}

-(UILabel *)sizeLab {
    if (!_sizeLab) {
        _sizeLab = [[UILabel alloc] initWithFrame:SARectMake(30, 0, 50, 40)];
        _sizeLab.font = kFont(14);
        _sizeLab.textColor = [UIColor blackColor];
        _sizeLab.textAlignment = NSTextAlignmentCenter;
    }
    return _sizeLab;
}

-(UIImageView *)lineImageV {
    if (!_lineImageV) {
        _lineImageV = [[UIImageView alloc] initWithFrame:SARectMake(18, 0, 339, 1)];
        _lineImageV.image = [UIImage imageNamed:@"shop_dottedLine"];
    }
    return _lineImageV;
}

-(UIButton *)cutBtn {
    if (!_cutBtn) {
        _cutBtn = [UIButton buttonWithType:UIButtonTypeCustom];
        [_cutBtn setImage:[UIImage imageNamed:@"shop_cutBtn"] forState:UIControlStateNormal];
        _cutBtn.frame = SARectMake(200, 0, 50, 40);
        _cutBtn.contentHorizontalAlignment = UIControlContentHorizontalAlignmentRight;
        [_cutBtn addTarget:self action:@selector(clickedCutBtn) forControlEvents:UIControlEventTouchUpInside];
    }
    return _cutBtn;
}

-(UIButton *)addBtn {
    if (!_addBtn) {
        _addBtn = [UIButton buttonWithType:UIButtonTypeCustom];
        [_addBtn setImage:[UIImage imageNamed:@"shop_addBtn"] forState:UIControlStateNormal];
        _addBtn.frame = SARectMake(308, 0, 50, 40);
        _addBtn.contentHorizontalAlignment = UIControlContentHorizontalAlignmentLeft;
        [_addBtn addTarget:self action:@selector(clickedAddBtn) forControlEvents:UIControlEventTouchUpInside];
    }
    return _addBtn;
}

-(UITextField *)numTf {
    if (!_numTf) {
        _numTf = [[UITextField alloc] initWithFrame:SARectMake(250, 0, 58, 40)];
        _numTf.delegate = self;
        _numTf.textAlignment = NSTextAlignmentCenter;
        _numTf.keyboardType = UIKeyboardTypeNumbersAndPunctuation;
        [_numTf addTarget:self action:@selector(textFieldEditChanged:) forControlEvents:UIControlEventEditingChanged];
    }
    return _numTf;
}

-(UILabel *)inventoryLab {
    if (!_inventoryLab) {
        _inventoryLab = [[UILabel alloc] initWithFrame:SARectMake(120, 0, 62, 40)];
        _inventoryLab.font = kFont(15);
        _inventoryLab.textColor = [UIColor blackColor];
        _inventoryLab.textAlignment = NSTextAlignmentCenter;
    }
    return _inventoryLab;
}

@end
